<?php
namespace app\member\controller;

class News extends Member
{
    /**
     * 新闻分类     2017-10-15
     */
    public function category()
    {
        if (request()->isPost()) {
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            $dataList = db(\tname::news_category)->where($where)->order('sort')->paginate(500);

            $this->assign('dataList', $dataList);
            $html = $this->fetch('news/form');

            return ajaxSuccess($html);
        }
        return $this->fetch();
    }

    /**
     * 新闻列表     2017-10-15
     */
    public function categoryadd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['uid'] = UID;

            $res = dataUpdate(\tname::news_category, $data);
            if (!$res) {
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $id = input('param.id', 0);
            $data = db(\tname::news_category)->find($id);

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * 新闻列表    2017-10-15
     */
    public function news()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'a.uid' => UID,
                'a.ishidden' => 0
            );
            if ($search['stime'] && !$search['etime']) {
                $where['a.create_time'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['a.create_time'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['a.create_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword']) {
                $where['a.title|c.name'] = array('like', '%' . $search['keyword'] . '%');
            }
            if ($search['ispublish'] != '') {
                $where['a.ispublish'] = $search['ispublish'];
            }

            $dataList = db(\tname::news)->alias('a')
                ->join('wechat_' . \tname::news_category . ' c', 'a.category_id = c.id', 'left')
                ->field('a.*,c.name category_name')
                ->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('news/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }
        return $this->fetch();
    }

    /**
     * 添加新闻    2017-10-15
     */
    public function newsadd()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $data['uid'] = UID;
            $data['imgpath'] = serializeMysql($data['imgpath']);
            $res = dataUpdate(\tname::news, $data);
            if (!$res) {
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $id = input('param.id', 0);
            $data = db(\tname::news)->find($id);

            if ($id) {
                $data['imgpath'] = serializeMysql($data['imgpath'], 1);
            }

            //新闻分类
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            $categoryList = db(\tname::news_category)->where($where)->order('sort')->select();

            $this->assign('categoryList', $categoryList);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * 新闻评价 2017-10-15
     */
    public function review()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'a.uid' => UID,
                'a.ishidden' => 0
            );
            if ($search['stime'] && !$search['etime']) {
                $where['a.create_time'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['a.create_time'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['a.create_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword'] !== null && $search['keyword'] !== '') {
                $where['n.title|a.content|a.nickname'] = array('like', '%' . $search['keyword'] . '%');
            }

            $dataList = db(\tname::news_review)->alias('a')
                ->join('wechat_' . \tname::news . ' n', 'a.news_id = n.id', 'left')
                ->field('a.*,n.title news_title')
                ->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('news/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }

        return $this->fetch();
    }

}